import uuid
from src.common.logger import getLogger
from src.modules.home.dao import user_dao

logger = getLogger()

def load_user_list():
    list = user_dao.select_user_list()
    logger.info(f"load_user_list list len: {len(list)}")
    return list

def load_user_page(args):
    page = user_dao.select_user_page(args)
    return page

def add_system_user(args):
    from ..model.user_model import User

    user = User()
    user.id = uuid.uuid4()
    user.user_name = args.get("user_name")
    user.password = args.get("password")
    user.phone_number = args.get("phone_number")
    user.email = args.get("email")
    user.resume = args.get("resume")
    user.photo = args.get("photo")
    user_dao.add_user(user)

def update_system_user(args):
    from ..model.user_model import User

    user = User()
    user.id = args.get("id")
    user.user_name = args.get("user_name")
    user.password = args.get("password")
    user.phone_number = args.get("phone_number")
    user.email = args.get("email")
    user.resume = args.get("resume")
    user.photo = args.get("photo")
    user_dao.update_user(user)

def delete_system_user(args):
    from ..model.user_model import User

    ids = args.get("ids")
    ids = ids.split(",")
    for id in ids:
        user = User.query.get(id)
        user_dao.delete_user(user)

def select_system_user(args):
    from ..model.user_model import User

    user = user_dao.select_system_user(args)
    user_dict = User.from_dict(user)
    if user:
        user_dict.visit += 1
        user_dao.update_user(user_dict)
    return user
